#include "SubscribedDialog.h"

SubscribedDialog::SubscribedDialog(User user)
{
    this->user = user;
    row = -1;

    layout_main = new QVBoxLayout;
    layout_column = new QHBoxLayout;
    list_books = new QListWidget();
    button_return = new QPushButton();
    label_list = new QVector<QLabel*>;

    list_id = new QVector<QString>;
    list_book_name = new QVector<QString>;
    list_user_name = new QVector<QString>;
    list_subscribe_time = new QVector<QString>;
}

SubscribedDialog::~SubscribedDialog()
{
    delete list_subscribe_time;
    delete list_user_name;
    delete list_book_name;
    delete list_id;

    for (QVector<QLabel*>::const_iterator iter = label_list->begin();iter != label_list->end(); iter++)
    {
        delete *iter;
    }
    delete label_list;

    delete button_return;
    delete list_books;
    delete layout_column;
    delete layout_main;
}

void SubscribedDialog::init_view_main()
{
    setFixedSize(480, 360);
    if(user.get_authority() == 0)
    {
        setWindowTitle("订阅记录");
    }
    else
    {
        setWindowTitle("我的订阅");
    }

    layout_main->setSpacing(10);    //设置控件距离
    setLayout(layout_main);

    for(int i = 0; i < 3; i++)
    {
        QLabel *label = new QLabel();
        label_list->push_back(label);
    }

    layout_column->addStretch(1);
    (*label_list)[0]->setText("书名");
    (*label_list)[0]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[0], 18);
    if(user.get_authority() != 0)
    {
        (*label_list)[1]->setText("作者");
        (*label_list)[1]->setStyleSheet("QLabel{font:10px;}");
        layout_column->addWidget((*label_list)[1], 12);
    }
    else
    {
        (*label_list)[1]->setText("订阅者");
        (*label_list)[1]->setStyleSheet("QLabel{font:10px;}");
        layout_column->addWidget((*label_list)[1], 12);
    }
    (*label_list)[2]->setText("订阅时间");
    (*label_list)[2]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[2], 6);
    layout_main->addLayout(layout_column);


    layout_main->addWidget(list_books);
    reflash_tabel();

    if(user.get_authority() != 0)
    {
        button_return->setText("归还");
        button_return->setStyleSheet("QLabel{font:15px;padding: 5px 15px 5px 15px;}");
        QObject::connect(button_return,&QPushButton::clicked,button_return,[&](){
            if(row != -1)
            {
                SQLHelper sql_helper;
                sql_helper.connect();

                QString book_id = (*list_id)[row];
                sql_helper.delete_table("subscribe", "book_id='" + book_id + "';");

                reflash_tabel();
            }
        }); //设置监听
        layout_main->addWidget(button_return);
    }
}

void SubscribedDialog::reflash_tabel()
{
    SQLHelper sql_helper;
    sql_helper.connect();


    if(user.get_authority() == 0)
    {
        QString sql = "SELECT books.name, user.name, subscribe.create_time FROM books RIGHT JOIN subscribe ON books.id = subscribe.book_id LEFT JOIN user on user.id = subscribe.user_id;";
        QSqlQuery* query = sql_helper.exec_sql(sql);

        if(query == NULL)
        {
            std::cout << "selete books failed!" << std::endl;
            return;
        }

        int count = 0;
        list_book_name->clear();
        list_user_name->clear();
        list_subscribe_time->clear();

        while (query->next()) {
            list_book_name->push_back(query->value(0).toString());
            list_user_name->push_back(query->value(1).toString());
            list_subscribe_time->push_back(query->value(2).toString());
            count++;
        }

        list_books->clear();
        for(int i = 0; i < count; i++)
        {
            QListWidgetItem *item=new QListWidgetItem(list_books);  // item
            QWidget *widget = new QWidget(list_books);  // item视图
            item->setSizeHint(QSize(120, 60));  // item大小
            QHBoxLayout *layout_item = new QHBoxLayout(widget); // item布局

            QLabel *label_book_name = new QLabel(widget);   // 书名label
            label_book_name->setText((*list_book_name)[i]);
            label_book_name->setStyleSheet("QLabel{font:10px;}");

            QLabel *label_user_name = new QLabel(widget); // 订阅者label
            label_user_name->setText((*list_user_name)[i]);
            label_user_name->setStyleSheet("QLabel{font:10px;}");

            QLabel *label_subscribe_time = new QLabel(widget); // 订阅时间label
            label_subscribe_time->setText((*list_subscribe_time)[i].left(10));
            label_subscribe_time->setStyleSheet("QLabel{font:10px;}");


            layout_item->addWidget(label_book_name, 9);
            layout_item->addWidget(label_user_name, 6);
            layout_item->addWidget(label_subscribe_time, 3);

            widget->setLayout(layout_item);

            list_books->setItemWidget(item,widget);
        }
    }
    else
    {
        QString sql = "SELECT books.id, books.name, books.author, subscribe.create_time FROM library_ms.books books INNER JOIN library_ms.subscribe subscribe ON books.id = subscribe.book_id WHERE subscribe.user_id='" + QString::number(user.get_id()) + "';";
        QSqlQuery* query = sql_helper.exec_sql(sql);

        if(query == NULL)
        {
            std::cout << "selete books failed!" << std::endl;
            return;
        }

        int count = 0;
        list_id->clear();


        while (query->next()) {
            list_id->push_back(query->value(0).toString());
            list_book_name->push_back(query->value(1).toString());
            list_user_name->push_back(query->value(2).toString());
            list_subscribe_time->push_back(query->value(3).toString());
            count++;
        }

        list_books->clear();
        for(int i = 0; i < count; i++)
        {
            QListWidgetItem *item=new QListWidgetItem(list_books);  // item
            QWidget *widget = new QWidget(list_books);  // item视图
            item->setSizeHint(QSize(120, 60));  // item大小
            QHBoxLayout *layout_item = new QHBoxLayout(widget); // item布局

            QLabel *label_book_name = new QLabel(widget);   // 书名label
            label_book_name->setText((*list_book_name)[i]);
            label_book_name->setStyleSheet("QLabel{font:10px;}");

            QLabel *label_user_name = new QLabel(widget); // 订阅者label
            label_user_name->setText((*list_user_name)[i]);
            label_user_name->setStyleSheet("QLabel{font:10px;}");

            QLabel *label_subscribe_time = new QLabel(widget); // 订阅时间label
            label_subscribe_time->setText((*list_subscribe_time)[i].left(10));
            label_subscribe_time->setStyleSheet("QLabel{font:10px;}");


            layout_item->addWidget(label_book_name, 9);
            layout_item->addWidget(label_user_name, 6);
            layout_item->addWidget(label_subscribe_time, 3);

            widget->setLayout(layout_item);

            list_books->setItemWidget(item,widget);
        }
    }

    QObject::connect(list_books,&QListWidget::itemClicked,list_books,[&]{
        row = list_books->currentRow(); // 选中的item
    });

    list_books->update();
}
